package com.example.dockerdemo.service.impl;

import com.example.dockerdemo.entity.Goods;
import com.example.dockerdemo.mapper.GoodsMapper;
import com.example.dockerdemo.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    public GoodsMapper goodsMapper;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Override
    public Goods selectById(Long goodsId) {
        Goods goods = goodsMapper.selectById(goodsId);
        if (goods!=null) {
            redisTemplate.opsForValue().set("goods:"+goodsId,goods);
        }
        return goodsMapper.selectById(goodsId);
    }

    @Override
    public Goods selectFromRedis(Long goodsId) {
        Goods goods = (Goods) redisTemplate.opsForValue().get("goods:" + goodsId);
//        if (goods == null) {
//            goods = goodsMapper.selectById(goodsId);
//            redisTemplate.opsForValue().set("goods:"+goodsId,goods);
//        }
//        goods = (Goods) redisTemplate.opsForValue().get("goods:" + goodsId);
        return goods;
    }
}
